# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.

library(tidyverse)
library(lubridate)
library(RColorBrewer)

# This analysis looks at overall Facebook spending over the election season.

# Ad Library, Full Dataset
df_raw <- read_csv("ad_library_2020.csv")

df <- 
  df_raw %>%
  mutate(
    ad_type = case_when(is_trump & is_biden ~ "Both",
                        is_trump ~ "Trump",
                        is_biden ~ "Biden"),
    month = as.Date(floor_date(ad_delivery_start_time, unit = "month")),
    week = as.Date(floor_date(ad_delivery_start_time, unit = "week")),
    spend_ub = ifelse(spend_ub == -1, 1000000, spend_ub)
  )

# Weekly Spend
gg_df_1 <-
  df %>%
  filter(week >= "2020-02-01") %>%
  group_by(week) %>%
  summarize(
    count = n(),
    spend_lb = sum(spend_lb),
    spend_ub = sum(spend_ub)
  ) 
  
g1 <- 
  ggplot(gg_df_1, aes(x = week, y = spend_lb)) +
  geom_line() +
  geom_point() +
  geom_vline(xintercept = as.Date("2020-11-03"), color = "red", linetype = "dashed") +
  geom_text(x=as.Date("2020-11-10"), y = 15e6, label="Election Day", colour="red", angle=-90) +
  ylab("Facebook Spending, Lower Bound (weekly)") +
  xlab("Date") +
  theme_bw() +
  theme(axis.title.x = element_blank()) +
  scale_y_continuous(labels=scales::dollar_format()) +
  scale_x_date(labels=scales::date_format(format = "%B %Y")) 

#ggsave("output/figure_C3.pdf", g1, height = 4, width= 6)
